﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace LdCms.BLL.Community
{
    using LdCms.EF.DbEntitiesContext;
    using LdCms.EF.DbModels;
    using LdCms.EF.DbStoredProcedure;
    using LdCms.IBLL.Community;
    using LdCms.IDAL.Community;
    using LdCms.Common.Json;
    using LdCms.Common.Security;
    using LdCms.Common.Extension;
    using LdCms.Common.Enum;

    public partial class ColumnService:BaseService<Ld_Community_Column>,IColumnService
    {
        private readonly IColumnDAL ColumnDAL;
        private readonly LdCmsDbEntitiesContext LdCmsDbEntitiesContext;
        public ColumnService(LdCmsDbEntitiesContext LdCmsDbEntitiesContext, IColumnDAL ColumnDAL)
        {
            this.LdCmsDbEntitiesContext = LdCmsDbEntitiesContext;
            this.ColumnDAL = ColumnDAL;
            this.Dal = ColumnDAL;
        }
        public override void SetDal()
        {
            Dal = ColumnDAL;
        }

        public bool IsExists(int systemId, string companyId,string columnId)
        {
            try
            {
                return IsExists(m => m.SystemID == systemId && m.CompanyID == companyId && m.ColumnID == columnId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool SaveColumn(Ld_Community_Column entity)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string columnId = entity.ColumnID;
                string columnName = entity.ColumnName;
                if (IsExists(m => m.SystemID == systemId && m.CompanyID == companyId && m.ColumnName == columnName))
                    throw new Exception("专栏名称不能重复！");
                if (string.IsNullOrEmpty(columnId))
                {
                    var communityColumn = PrimaryKeyHelper.PrimaryKeyType.CommunityColumn;
                    var primaryKeyLen = PrimaryKeyHelper.PrimaryKeyLen.V1;
                    string createColumnId = PrimaryKeyHelper.MakePrimaryKey(communityColumn, primaryKeyLen);
                    entity.ColumnID = createColumnId;
                }
                entity.IsShow = entity.IsShow.ToBool();
                entity.IsSend = entity.IsSend.ToBool();
                entity.IsReply = entity.IsReply.ToBool();
                entity.IsLock = entity.IsLock.ToBool();
                entity.Sort = entity.Sort.ToInt();
                entity.State = entity.State.ToBool();
                entity.CreateDate = DateTime.Now;
                return Add(entity);
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool SaveColumn(Ld_Community_Column entity, List<Ld_Community_ColumnPermission> lists)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string columnId = entity.ColumnID;
                string columnName = entity.ColumnName;
                if (IsExists(m => m.SystemID == systemId && m.CompanyID == companyId && m.ColumnName == columnName))
                    throw new Exception("专栏名称不能重复！");
                if (string.IsNullOrEmpty(columnId))
                {
                    var communityColumn = PrimaryKeyHelper.PrimaryKeyType.CommunityColumn;
                    var primaryKeyLen = PrimaryKeyHelper.PrimaryKeyLen.V1;
                    string createColumnId = PrimaryKeyHelper.MakePrimaryKey(communityColumn, primaryKeyLen);
                    entity.ColumnID = createColumnId;
                }

                bool isShow = lists.Where(m => m.TypeID.Value == (int)ParamEnum.ColumnPermission.查看权限).Count() > 0;
                bool isSend = lists.Where(m => m.TypeID.Value == (int)ParamEnum.ColumnPermission.发帖权限).Count() > 0;
                bool isReply = lists.Where(m => m.TypeID.Value == (int)ParamEnum.ColumnPermission.回帖权限).Count() > 0;

                entity.IsShow = isShow;
                entity.IsSend = isSend;
                entity.IsReply = isReply;
                entity.IsLock = entity.IsLock.ToBool();
                entity.Sort = entity.Sort.ToInt();
                entity.State = entity.State.ToBool();
                entity.CreateDate = DateTime.Now;

                List<Ld_Community_ColumnPermission> details = new List<Ld_Community_ColumnPermission>();
                foreach (var m in lists)
                {
                    var communityColumn = PrimaryKeyHelper.PrimaryKeyType.CommunityColumnPermission;
                    var primaryKeyLen = PrimaryKeyHelper.PrimaryKeyLen.V1;
                    string createColumnPermissionId = PrimaryKeyHelper.MakePrimaryKey(communityColumn, primaryKeyLen);
                    m.SystemID = systemId;
                    m.CompanyID = companyId;
                    m.ColumnID = entity.ColumnID;
                    m.PermissionID = createColumnPermissionId;
                    m.TypeID = m.TypeID;
                    m.TypeName = m.TypeName;
                    m.RankID = m.RankID;
                    m.RankName = m.RankName;
                    m.State = m.State.ToBool();
                    m.CreateDate = DateTime.Now;
                    details.Add(m);
                }
                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                using (var db = dbContext.DbEntities())
                {
                    using (var trans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            dbContext.Add(entity);
                            dbContext.Add(details);
                            intnum = db.SaveChanges();
                            trans.Commit();
                        }
                        catch (Exception)
                        {
                            trans.Rollback();
                        }
                        return intnum > 0;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool UpdateColumn(Ld_Community_Column entity)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string columnId = entity.ColumnID;
                string columnName = entity.ColumnName;
                if (IsExists(m => m.SystemID == systemId && m.CompanyID == companyId && m.ColumnID != columnId && m.ColumnName == columnName))
                    throw new Exception("专栏名称不能重复！");
                entity.IsShow = entity.IsShow.ToBool();
                entity.IsSend = entity.IsSend.ToBool();
                entity.IsReply = entity.IsReply.ToBool();
                entity.IsLock = entity.IsLock.ToBool();
                entity.Sort = entity.Sort.ToInt();
                entity.State = entity.State.ToBool();
                entity.CreateDate = DateTime.Now;
                return Update(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool UpdateColumn(Ld_Community_Column entity, List<Ld_Community_ColumnPermission> lists)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string columnId = entity.ColumnID;
                string columnName = entity.ColumnName;
                if (IsExists(m => m.SystemID == systemId && m.CompanyID == companyId && m.ColumnID != columnId && m.ColumnName == columnName))
                    throw new Exception("专栏名称不能重复！");
                bool isShow = lists.Where(m => m.TypeID.Value == (int)ParamEnum.ColumnPermission.查看权限).Count() > 0;
                bool isSend = lists.Where(m => m.TypeID.Value == (int)ParamEnum.ColumnPermission.发帖权限).Count() > 0;
                bool isReply = lists.Where(m => m.TypeID.Value == (int)ParamEnum.ColumnPermission.回帖权限).Count() > 0;
                entity.IsShow = isShow;
                entity.IsSend = isSend;
                entity.IsReply = isReply;
                entity.IsLock = entity.IsLock.ToBool();
                entity.Sort = entity.Sort.ToInt();
                entity.State = entity.State.ToBool();
                entity.CreateDate = DateTime.Now;

                List<Ld_Community_ColumnPermission> details = new List<Ld_Community_ColumnPermission>();
                foreach (var m in lists)
                {
                    var communityColumn = PrimaryKeyHelper.PrimaryKeyType.CommunityColumnPermission;
                    var primaryKeyLen = PrimaryKeyHelper.PrimaryKeyLen.V1;
                    string createColumnPermissionId = PrimaryKeyHelper.MakePrimaryKey(communityColumn, primaryKeyLen);
                    m.SystemID = systemId;
                    m.CompanyID = companyId;
                    m.ColumnID = columnId;
                    m.PermissionID = createColumnPermissionId;
                    m.TypeID = m.TypeID;
                    m.TypeName = m.TypeName;
                    m.RankID = m.RankID;
                    m.RankName = m.RankName;
                    m.State = m.State.ToBool();
                    m.CreateDate = DateTime.Now;
                    details.Add(m);
                }
                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                using (var db = dbContext.DbEntities())
                {
                    using (var trans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            dbContext.Delete<Ld_Community_ColumnPermission>(m => m.SystemID == systemId && m.CompanyID == companyId && m.ColumnID == columnId);
                            dbContext.Update(entity);
                            dbContext.Add(details);
                            intnum = db.SaveChanges();
                            trans.Commit();
                        }
                        catch (Exception)
                        {
                            trans.Rollback();
                        }
                        return intnum > 0;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool UpdateColumnState(int systemId, string companyId, string columnId, bool state)
        {
            try
            {
                if (!IsExists(systemId, companyId, columnId))
                    throw new Exception("ID无效！");
                var entity = GetColumn(systemId, companyId, columnId);
                entity.State = state;
                return Update(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool DeleteColumn(int systemId, string companyId, string columnId)
        {
            try
            {
                if (!IsExists(systemId, companyId, columnId))
                    throw new Exception("ID无效！");

                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                using (var db = dbContext.DbEntities())
                {
                    using (var trans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            dbContext.Delete<Ld_Community_ColumnPermission>(m => m.SystemID == systemId && m.CompanyID == companyId && m.ColumnID == columnId);
                            dbContext.Delete<Ld_Community_Column>(m => m.SystemID == systemId && m.CompanyID == companyId && m.ColumnID == columnId);
                            intnum = db.SaveChanges();
                            trans.Commit();
                        }
                        catch (Exception)
                        {
                            trans.Rollback();
                        }
                        return intnum > 0;
                    }
                }
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Community_Column GetColumn(int systemId, string companyId, string columnId)
        {
            try
            {
                return Find(m => m.SystemID == systemId && m.CompanyID == companyId && m.ColumnID == columnId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Community_Column> GetColumnAll(int systemId, string companyId)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Community_Column>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId);
                return FindList(expression, m => m.Sort, true).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Community_Column> GetColumnAll(int systemId, string companyId, bool state)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Community_Column>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.State == state);
                return FindList(expression, m => m.Sort, true).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public int CountColumn(int systemId, string companyId)
        {
            try
            {
                return Count(m => m.SystemID == systemId && m.CompanyID == companyId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

    }
}
